package java_concurrency_program_design_in_practice.stop_suspend_resume;

/**
 * suspend在导致线程暂停的同时，并不会去释放任何资源<br/>
 * 如果resume()操作意外的在suspend()前执行了，那么被挂起的线程可能很难有机会被继续执行<br/>
 * 本实例中，t2.resume()在t2.suspend()之前执行了，导致线程t2被永远挂起，且并没有释放任何资源
 * @author lixiangke
 * @date 2017/12/8
 */
public class SuspendResumeTest {
    public static Object u = new Object();
    static ChangeObjectThread t1 = new ChangeObjectThread("t1");
    static ChangeObjectThread t2 = new ChangeObjectThread("t2");

    public static class ChangeObjectThread extends Thread {
        public ChangeObjectThread(String name) {
            super.setName(name);
        }

        @Override
        public void run() {
            synchronized (u) {
                System.out.println("in " + getName());
                Thread.currentThread().suspend();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        t1.start();
        Thread.sleep(100);
        t2.start();
        t1.resume();
        t2.resume();
        t1.join();
        System.out.println("t1 Over");
        t2.join();
        System.out.println("t2 Over");
    }
}
